export function wxToLogin() {
  // let oUrl = window.location.href;
  // const redirect_uri = encodeURIComponent(oUrl); 
  // const appid = "wx68d94c7b5ee4bfbc";
  // window.location = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`;

  throw "stop";
}

export function handleTree(data, id, parentId, children, rootId) {
  id = id || "id";
  parentId = parentId || "parentId";
  children = children || "children";
  rootId =
    rootId ||
    Math.min.apply(
      Math,
      data.map((item) => {
        return item[parentId];
      })
    ) ||
    0;
  //对源数据深度克隆
  const cloneData = JSON.parse(JSON.stringify(data));
  //循环所有项
  const treeData = cloneData
    .filter((father) => {
      let branchArr = cloneData
        .filter((child) => {
          //返回每一项的子级数组
          return father[id] === child[parentId];
        })
        .sort((a, b) => {
          if (a.sort >= 0) {
            return a.sort - b.sort;
          }
          return 0;
        });
      branchArr.length > 0 ? (father.children = branchArr) : "";
      //返回第一层
      return father[parentId] === rootId;
    })
    .sort((a, b) => {
      if (a.sort >= 0) {
        return a.sort - b.sort;
      }
      return 0;
    });
  return treeData != "" ? treeData : data;
}

export const isHttp = (url) => {
  return url.indexOf("http") > -1;
};


export const isIOS = () => {
  return /iphone|ipad|ipod/.test(navigator.userAgent.toLowerCase());
}

export const getProductTypes = (list, parentId) => {
  if(!parentId) return "";
  const level2 = list.filter((x) => x.pid == parentId).map((x) => x.id);
  const level3 = list
    .filter((x) => level2.indexOf(x.pid) != -1)
    .map((x) => x.id);
  const productTypeList = [parentId,...level2, ...level3].join(",");
  return productTypeList;
};
